{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-08T08:18:12.003148Z",
     "start_time": "2025-01-08T08:18:11.999740Z"
    }
   },
   "source": [
    "# 导入库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import random"
   ],
   "outputs": [],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Pandas 函数应用",
   "id": "7e10137b7494a652"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:06:08.685590Z",
     "start_time": "2025-01-08T08:06:08.681227Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Numpy ufuncs 函数\n",
    "# randn() 产生正态分布随机数\n",
    "\n",
    "df = pd.DataFrame(np.random.randn(5, 4) - 1)\n",
    "\n",
    "# 绝对值函数 abs()\n",
    "print(np.abs(df))"
   ],
   "id": "52cf8b85a016e68d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0  0.497036  0.263212  1.201851  1.524030\n",
      "1  1.484494  0.308630  0.692596  0.194127\n",
      "2  0.185874  0.051659  2.165811  1.305023\n",
      "3  1.086443  2.166262  1.092670  0.272218\n",
      "4  0.810298  0.470735  0.725499  0.446288\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:08:15.524607Z",
     "start_time": "2025-01-08T08:08:15.518721Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 匿名函数 lambda x: x**2\n",
    "#.apply() 函数应用到 DataFrame 上\n",
    "# 最大值函数 max(),作用在列上\n",
    "print(df.apply(lambda x: max(x)))\n",
    "\n",
    "# 最大值函数 max(),作用在行上\n",
    "print(df.apply(lambda x: max(x), axis=1))\n",
    "\n",
    "# 作用到每个元素上\n",
    "# .map：对每个元素进行操作，返回一个新的Series\n",
    "print(df.map(lambda x: '%.2f' % x))"
   ],
   "id": "5a31fca6cad34aa2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.185874\n",
      "1    0.470735\n",
      "2    0.692596\n",
      "3    0.194127\n",
      "dtype: float64\n",
      "0   -0.263212\n",
      "1    0.692596\n",
      "2   -0.051659\n",
      "3   -0.272218\n",
      "4    0.470735\n",
      "dtype: float64\n",
      "       0      1      2      3\n",
      "0  -0.50  -0.26  -1.20  -1.52\n",
      "1  -1.48  -0.31   0.69   0.19\n",
      "2  -0.19  -0.05  -2.17  -1.31\n",
      "3  -1.09  -2.17  -1.09  -0.27\n",
      "4  -0.81   0.47  -0.73  -0.45\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 索引排序",
   "id": "fc34aeaff0fbc416"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:12:50.146338Z",
     "start_time": "2025-01-08T08:12:50.138822Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成随机索引\n",
    "s = pd.Series(range(10, 15), index=np.random.randint(5, size=5))\n",
    "print(s)\n",
    "\n",
    "# 按照索引排序\n",
    "s_sorted = s.sort_index()\n",
    "print(s_sorted)\n",
    "\n",
    "# 索引值不唯一会报错,位置索引可以解决\n",
    "#loc索引值唯一时可以切片\n",
    "print(s.iloc[0:3])"
   ],
   "id": "d842714fc6d28703",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2    10\n",
      "3    11\n",
      "3    12\n",
      "2    13\n",
      "3    14\n",
      "dtype: int64\n",
      "2    10\n",
      "2    13\n",
      "3    11\n",
      "3    12\n",
      "3    14\n",
      "dtype: int64\n",
      "2    10\n",
      "3    11\n",
      "3    12\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:16:21.127489Z",
     "start_time": "2025-01-08T08:16:21.120691Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df4 = pd.DataFrame(np.random.randn(5, 5),\n",
    "                   index=np.random.randint(5, size=5),\n",
    "                   columns=np.random.randint(5, size=5))\n",
    "print(df4)\n",
    "\n",
    "#轴零是行索引排序\n",
    "print('按行索引排序:')\n",
    "df4_isort = df4.sort_index(axis=0, ascending=True)\n",
    "print(df4_isort)\n",
    "\n",
    "#轴1是列索引排序\n",
    "print('按列索引排序:')\n",
    "df4_isort = df4.sort_index(axis=1, ascending=True)\n",
    "print(df4_isort)"
   ],
   "id": "3c44b35af9491366",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          2         3         0         1         4\n",
      "2 -1.299796  0.900463  0.779991  0.213173 -0.648900\n",
      "3  0.148376 -1.297533  2.043670  0.145174 -0.877858\n",
      "0 -0.287317 -1.884017  0.204416 -1.987276 -0.457581\n",
      "2 -0.371520 -0.977100  1.680384 -0.923104 -1.249518\n",
      "3 -1.658694  0.083173 -1.210567  0.005423 -1.262065\n",
      "按行索引排序:\n",
      "          2         3         0         1         4\n",
      "0 -0.287317 -1.884017  0.204416 -1.987276 -0.457581\n",
      "2 -1.299796  0.900463  0.779991  0.213173 -0.648900\n",
      "2 -0.371520 -0.977100  1.680384 -0.923104 -1.249518\n",
      "3  0.148376 -1.297533  2.043670  0.145174 -0.877858\n",
      "3 -1.658694  0.083173 -1.210567  0.005423 -1.262065\n",
      "按列索引排序:\n",
      "          0         1         2         3         4\n",
      "2  0.779991  0.213173 -1.299796  0.900463 -0.648900\n",
      "3  2.043670  0.145174  0.148376 -1.297533 -0.877858\n",
      "0  0.204416 -1.987276 -0.287317 -1.884017 -0.457581\n",
      "2  1.680384 -0.923104 -0.371520 -0.977100 -1.249518\n",
      "3 -1.210567  0.005423 -1.658694  0.083173 -1.262065\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 按值排序",
   "id": "11cd174a045c5967"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:23:30.437891Z",
     "start_time": "2025-01-08T08:23:30.432885Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 按值排序\n",
    "\n",
    "# 创建DataFrame\n",
    "l = [random.randint(0, 100) for i in range(24)]\n",
    "df = pd.DataFrame(np.array(l).reshape(6, 4))\n",
    "print(df)\n",
    "\n",
    "# 按轴0排序,by列索引名,是column的值，按列排序，交换行\n",
    "df_vsort = df.sort_values(by=3, axis=0, ascending=True)\n",
    "print(df_vsort)\n",
    "\n",
    "# 按轴1排序，by后行索引名，是index的值，按行排序，交换的是列\n",
    "df_vsort = df.sort_values(by=2, axis=1, ascending=True)\n",
    "print(df_vsort)"
   ],
   "id": "de5451e4de891c4a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "0  39  31  49  73\n",
      "1   3  72  23   6\n",
      "2   0  40  21  19\n",
      "3  33  38  82  14\n",
      "4  50  13  22   6\n",
      "5   9  99  27   9\n",
      "    0   1   2   3\n",
      "1   3  72  23   6\n",
      "4  50  13  22   6\n",
      "5   9  99  27   9\n",
      "3  33  38  82  14\n",
      "2   0  40  21  19\n",
      "0  39  31  49  73\n",
      "    0   3   2   1\n",
      "0  39  73  49  31\n",
      "1   3   6  23  72\n",
      "2   0  19  21  40\n",
      "3  33  14  82  38\n",
      "4  50   6  22  13\n",
      "5   9   9  27  99\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 处理缺失值",
   "id": "b53ca7c43266878a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:48:50.672512Z",
     "start_time": "2025-01-08T08:48:50.667488Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建DataFrame\n",
    "df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],\n",
    "                        [np.nan, 4., np.nan], [1., 2., 3.]])\n",
    "\n",
    "# isnull来判断是否有空的数据\n",
    "print(df_data.isnull())\n",
    "\n",
    "# 计算df_data缺失率\n",
    "print(df_data.isnull().sum() / len(df_data))"
   ],
   "id": "4f628156c956680f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n",
      "0    0.25\n",
      "1    0.00\n",
      "2    0.50\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 删除缺失值",
   "id": "51b5cef28729130"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:49:40.287023Z",
     "start_time": "2025-01-08T08:49:40.281516Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#默认一个样本，任何一个特征缺失，就删除\n",
    "#inplace True是修改的是原有的df\n",
    "\n",
    "#subset=[0]是指按第一列来删除,第一列有空值就删除对应的行\n",
    "#用的不多，用在某个特征缺失太多时，才会进行删除\n",
    "print(df_data.dropna(subset=[0]))"
   ],
   "id": "ccecd996bb47496d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0  0.790065  1.260372 -1.777368\n",
      "1  1.000000  2.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 填充缺失值",
   "id": "f29838374b7dfc08"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:53:02.403387Z",
     "start_time": "2025-01-08T08:53:02.396194Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 填充方式有很多种，fillna()函数可以实现\n",
    "# 填充方式有：\n",
    "# 1. 0：用0填充\n",
    "# 2. 平均值：用平均值填充\n",
    "# 3. 前一个值：用前一个值填充\n",
    "# 4. 后一个值：用后一个值填充\n",
    "# 5. 任意值：用任意值填充\n",
    "\n",
    "# 填充0\n",
    "print(df_data.fillna(0))\n",
    "\n",
    "# 填充平均值\n",
    "print(df_data.fillna(df_data.mean()))\n",
    "\n",
    "# 填充前一个值\n",
    "print(df_data.ffill())\n",
    "\n",
    "# 填充后一个值        \n",
    "print(df_data.bfill())\n",
    "\n",
    "# 填充任意值\n",
    "print(df_data.fillna(value=100))"
   ],
   "id": "82f5500d3a817cac",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0  0.790065  1.260372 -1.777368\n",
      "1  1.000000  2.000000  0.000000\n",
      "2  0.000000  4.000000  0.000000\n",
      "3  1.000000  2.000000  3.000000\n",
      "          0         1         2\n",
      "0  0.790065  1.260372 -1.777368\n",
      "1  1.000000  2.000000  0.611316\n",
      "2  0.930022  4.000000  0.611316\n",
      "3  1.000000  2.000000  3.000000\n",
      "          0         1         2\n",
      "0  0.790065  1.260372 -1.777368\n",
      "1  1.000000  2.000000 -1.777368\n",
      "2  1.000000  4.000000 -1.777368\n",
      "3  1.000000  2.000000  3.000000\n",
      "          0         1         2\n",
      "0  0.790065  1.260372 -1.777368\n",
      "1  1.000000  2.000000  3.000000\n",
      "2  1.000000  4.000000  3.000000\n",
      "3  1.000000  2.000000  3.000000\n",
      "            0         1           2\n",
      "0    0.790065  1.260372   -1.777368\n",
      "1    1.000000  2.000000  100.000000\n",
      "2  100.000000  4.000000  100.000000\n",
      "3    1.000000  2.000000    3.000000\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#依次拿到每一列\n",
    "for i in df_data.columns:\n",
    "    print(df_data.loc[:, i])"
   ],
   "id": "9c50f7935867c442"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
